<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将一个或多个单元压入数组的末尾（入栈）</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array-push" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_push</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_push</span> &mdash; <span class="dc-title">
   将一个或多个单元压入数组的末尾（入栈）
  </span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.array-push-description">
  <h3 class="title">说明</h3>
 <div class="methodsynopsis dc-description">
  <span class="methodname"><strong>array_push</strong></span>
   ( <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$array</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value1</code></span>
  [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
 ] ) : <span class="type">int</span></div>

 <p class="para rdfs-comment">
  <span class="function"><strong>array_push()</strong></span> 将
  <code class="parameter">array</code> 当成一个栈，并将传入的变量压入
  <code class="parameter">array</code> 的末尾。<code class="parameter">array</code>
  的长度将根据入栈变量的数目增加。和如下效果相同：
  <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">$var</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
     </div>

     并对每个传入的值重复以上动作。
    </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      如果用 <span class="function"><strong>array_push()</strong></span> 来给数组增加一个单元，还不如用
      <em>$array[] = </em>，因为这样没有调用函数的额外负担。
    </span>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      如果第一个参数不是数组，<span class="function"><strong>array_push()</strong></span>
      将发出一条警告。这和
    <em>$var[]</em> 的行为不同，后者会新建一个数组。
       </span>
    </p></blockquote>
   </div>

   <div class="refsect1 parameters" id="refsect1-function.array-push-parameters">
    <h3 class="title">参数</h3>
    <p class="para">
     <dl>

      
       <dt>
<code class="parameter">array</code></dt>

       <dd>

        <p class="para">
         输入的数组。
        </p>
       </dd>

      
      
       <dt>
<code class="parameter">value1</code></dt>

       <dd>

        <p class="para">
         要压入 <code class="parameter">array</code> 末尾的第一个值。
        </p>
       </dd>

      
     </dl>

    </p>
   </div>

   <div class="refsect1 returnvalues" id="refsect1-function.array-push-returnvalues">
    <h3 class="title">返回值</h3>
    <p class="para">
     返回处理之后数组的元素个数。
    </p>
   </div>

   <div class="refsect1 examples" id="refsect1-function.array-push-examples">
    <h3 class="title">范例</h3>
    <p class="para">
     <div class="example" id="example-6057">
      <p><strong>Example #1 <span class="function"><strong>array_push()</strong></span> 例子</strong></p>
      <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$stack&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"banana"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_push</span><span style="color: #007700">(</span><span style="color: #0000BB">$stack</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"raspberry"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$stack</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; orange
    [1] =&gt; banana
    [2] =&gt; apple
    [3] =&gt; raspberry
)
</pre></div>
    </div>
   </div>
     </p>
   </div>

   <div class="refsect1 seealso" id="refsect1-function.array-push-seealso">
    <h3 class="title">参见</h3>
    <p class="para">
     <ul class="simplelist">
      <li class="member"><span class="function"><a href="array_pop.html" class="function" rel="rdfs-seeAlso">array_pop()</a> - 弹出数组最后一个单元（出栈）</span></li>
      <li class="member"><span class="function"><a href="array_shift.html" class="function" rel="rdfs-seeAlso">array_shift()</a> - 将数组开头的单元移出数组</span></li>
      <li class="member"><span class="function"><a href="array_unshift.html" class="function" rel="rdfs-seeAlso">array_unshift()</a> - 在数组开头插入一个或多个单元</span></li>
     </ul>
    </p>
   </div>

</div></div></div></body></html>